/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.common.tool.security.user;



import io.renren.common.tool.contact.EqsContact;
import io.renren.common.tool.redis.UserDetailRedis;
import io.renren.common.utils.HttpContextUtils;
import io.renren.common.utils.SpringContextUtils;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
public class SecurityUser {
    private static UserDetailRedis userDetailRedis;
    static {
        userDetailRedis = SpringContextUtils.getBean(UserDetailRedis.class);
    }

    /**
     * 获取用户信息
     */
    public static UserDetail getUser(){
        String userId = getUserId();
        if(StringUtils.isBlank(userId)){
            return null;
        }

        UserDetail user = userDetailRedis.get(userId);
        return user;
    }

    /**
     * 获取用户ID
     */
    public static String getUserId() {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        if(request == null){
            return null;
        }

        String userId = request.getHeader(EqsContact.USER_KEY);
        return userId;
    }

    /**
     * 获取部门ID
     */
    public static String getDeptId() {
        UserDetail user = getUser();
        if(user == null){
            return null;
        }

        return user.getDeptId();
    }
}